/**
 * 
 */
package t4j.generics;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import t4j.typeinfo.pets.Cat;
import t4j.typeinfo.pets.Dog;
import t4j.typeinfo.pets.Pet;


public class CheckedList {

	static void oldStyleMethod(List probablyDogs) {
		probablyDogs.add(new Cat());
	}
	
	public static void main(String[] args) {
		List<Dog> dogs1 = new ArrayList<Dog>();
		
		//这里不会，当对象从容器中拿出才会报错
		oldStyleMethod(dogs1);
		
		//这里拿出对象，报ClassCastException，因为实际插入的是Cat类
//		for(Dog a : dogs1)
//			System.out.println(a);
		
		//强制容器只能插入Dog类
		List<Dog> dogs2 = Collections.checkedList(new ArrayList<Dog>(), Dog.class);
		
		try{
			oldStyleMethod(dogs2);
		}catch(Exception e){
			System.out.println(e);
		}
		
		List<Pet> pets = Collections.checkedList(new ArrayList<Pet>(), Pet.class);
		
		pets.add(new Dog());
		pets.add(new Cat());
		
	}
}
